﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary.MoveItems
{
    /// <summary>
    /// Item.xaml 的交互逻辑
    /// </summary>
    public partial class Item : UserControl
    {
        bool IsMouseLeftButtonDown = false;
        Point StartPoint;
        Point CurrentPoint;
        System.Windows.Controls.Grid ItemCanMove = null;

        System.Timers.Timer ItemCanMoveTimer;


        public double ItemHight
        {
            get { return (double)GetValue(ItemHightProperty); }
            set { SetValue(ItemHightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemHight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemHightProperty =
            DependencyProperty.Register("ItemHight", typeof(double), typeof(Item), new PropertyMetadata(0d));



        public double BackgroundHight
        {
            get { return (double)GetValue(BackgroundHightProperty); }
            set { SetValue(BackgroundHightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BackgroundHight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundHightProperty =
            DependencyProperty.Register("BackgroundHight", typeof(double), typeof(Item), new PropertyMetadata(0d));




        public bool IsAutoMove
        {
            get { return (bool)GetValue(IsAutoMoveProperty); }
            set { SetValue(IsAutoMoveProperty, value); TimerStart(); }
        }

        // Using a DependencyProperty as the backing store for IsAutoMove.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsAutoMoveProperty =
            DependencyProperty.Register("IsAutoMove", typeof(bool), typeof(Item), new PropertyMetadata(false));




        public Item()
        {
            InitializeComponent();
            this.Loaded += Item_Loaded;
            this.Unloaded += Item_Unloaded;
        }              

        private void Item_Loaded(object sender, RoutedEventArgs e)
        {
            ItemCanMoveTimer = new System.Timers.Timer();
            ItemCanMoveTimer.Interval = 500;
            ItemCanMoveTimer.Elapsed += ItemCanMoveTimer_Elapsed;
            TimerStart();
        }

        private void Item_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        private void TimerStart()
        {
            ItemCanMoveTimer?.Start();
        }

        private void ItemCanMoveTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    //你想要做的操作
                    if (!IsAutoMove)
                    {
                        ItemCanMoveTimer.Stop();
                    }
                    AutoMove();
                }));
            }
            
        }

        public string Name {  get; set; }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ItemCanMove = (System.Windows.Controls.Grid)sender;
            Target_MouseDown(sender, e);
        }
        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsMouseLeftButtonDown = false;
        }
        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseLeftButtonDown)
            {
                Mouse.SetCursor(Cursors.ScrollNS);

                CurrentPoint = Mouse.GetPosition(null);

                var chaValue = CurrentPoint.Y - StartPoint.Y;

                
                var original = ItemCanMove.Margin.Top;

                var ToDoValue = original + chaValue;
                
                ToDoValue = CheckValue(ToDoValue);

                ItemCanMove.Margin = new Thickness(0, ToDoValue, 0, 0);

                StartPoint = CurrentPoint;
            }
            else
            {
                Mouse.SetCursor(Cursors.Hand);
            }
        }


        void Target_MouseDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseLeftButtonDown = true;
            StartPoint = Mouse.GetPosition(null);
            CurrentPoint = StartPoint;            
        }


        void AutoMove()
        {
            if (ItemCanMove != null)
            {
                var radom = new Random();
                var rValue = radom.Next(0, 200);
                try
                {

                    ItemCanMove.Margin = new Thickness(0, CheckValue(rValue), 0, 0);
                }
                catch (Exception ex)
                {

                    
                }
            }
            


        }

        private double CheckValue(double value)
        {
            if (value < 0)
            {
                value = 0; IsMouseLeftButtonDown = false;
            }
            if (value > (BackgroundHight - ItemHight))
            {
                value = BackgroundHight - ItemHight; IsMouseLeftButtonDown = false;
            }

            return value;
        }
    }
}
